import _ from 'lodash'
export function useTools() {
  const objToOne = (obj): any => {
    const tmpData = {}
    for (const index in obj) {
      if (typeof obj[index] === 'object' && !isArrayFn(obj[index])) {
        const resObj = objToOne(obj[index])
        Object.assign(tmpData, resObj) // 这里使用对象合并
      } else {
        tmpData[index] = obj[index]
      }
    }
    return tmpData
  }

  const isArrayFn = (value) => {
    if (typeof Array.isArray === 'function') {
      return Array.isArray(value)
    } else {
      return Object.prototype.toString.call(value) === '[object Array]'
    }
  }

  const getDomTopLeftById = (id) => {
    const dom = document.getElementById(id)
    let top = 0
    let left = 0
    if (dom != null) {
      top = dom.getBoundingClientRect().top
      left = dom.getBoundingClientRect().left
    }
    return { top, left }
  }

  const setWidgetConfigValue = (config, configValue) => {
    // 循环遍历前非空判断
    if (config) {
      config.forEach((item) => {
        if (_.isPlainObject(item)) {
          configValue[item.name] = item.value
        }
        if (_.isArray(item)) {
          item.forEach((itemChild) => {
            itemChild.list.forEach((ev) => {
              configValue[ev.name] = ev.value
            })
          })
        }
      })
    }
  }

  const getWidgetConfigValue = (widgetJson) => {
    setWidgetConfigValue(widgetJson.options.setup, widgetJson.value.setup)
    setWidgetConfigValue(widgetJson.options.position, widgetJson.value.position)
    setWidgetConfigValue(widgetJson.options.data, widgetJson.value.data)

    return widgetJson
  }

  const setDefaultWidgetConfigValue = (data, option) => {
    setConfigValue(data.setup, option.setup)
    setConfigValue(data.position, option.position)
    setConfigValue(data.data, option.data)
    return option
  }

  const setConfigValue = (objValue, setup) => {
    Object.keys(objValue).forEach((key) => {
      setup.forEach((item) => {
        if (_.isPlainObject(item) && key === item.name) {
          item.value = objValue[key]
        } else if (Array.isArray(item)) {
          item.forEach((child) => {
            child.list?.forEach((el) => {
              if (el.name === key) {
                el.value = objValue[key]
              }
            })
          })
        }
      })
    })
  }

  const toRgba = (color: string, alpha: number): string => {
    let r = 0,
      g = 0,
      b = 0

    if (color.startsWith('#')) {
      // 去除 #
      const hex = color.replace('#', '')

      // 3 位缩写（#abc）
      if (hex.length === 3) {
        r = parseInt(hex[0] + hex[0], 16)
        g = parseInt(hex[1] + hex[1], 16)
        b = parseInt(hex[2] + hex[2], 16)
      }
      // 6 位标准写法（#aabbcc）
      else if (hex.length === 6) {
        r = parseInt(hex.substring(0, 2), 16)
        g = parseInt(hex.substring(2, 4), 16)
        b = parseInt(hex.substring(4, 6), 16)
      } else {
        throw new Error('Invalid hex color format')
      }
    } else if (color.startsWith('rgb')) {
      // 处理 rgb(r, g, b)
      const matches = color.match(/\d+/g)
      if ((matches && matches.length === 3) || (matches && matches.length === 4)) {
        r = parseInt(matches[0])
        g = parseInt(matches[1])
        b = parseInt(matches[2])
      } else {
        throw new Error('Invalid RGB format')
      }
    } else {
      throw new Error('Unsupported color format')
    }

    return `rgba(${r}, ${g}, ${b}, ${alpha})`
  }

  return {
    objToOne,
    isArrayFn,
    getDomTopLeftById,
    getWidgetConfigValue,
    setDefaultWidgetConfigValue,
    setConfigValue,
    toRgba
  }
}
